﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class UIReward : MonoBehaviour {

    public GameObject icon1, icon2;
    public GameObject eff1, eff2;
    public GameObject reward1, reward2;
    public void Start()
    {
        UGUIEventListener.Get(reward1).onClick = (obj) =>
        {
            Reset();
            LvController.Instance.ToNextLv();
            this.gameObject.SetActive(false);
        };
        UGUIEventListener.Get(reward2).onClick = (obj) =>
        {
            Reset();
            LvController.Instance.ToNextLv();
            this.gameObject.SetActive(false);
        };
    }
    
    public void Init()
    {
        Reset();
        eff1.GetComponent<ParticleSystem>().Play();
        eff2.GetComponent<ParticleSystem>().Play();

        Sprite sp1 = Instantiate(Resources.Load<Sprite>("Image/" + LvController.Instance.itemNames[LvController.Instance.itemAdd[LvController.Instance.currentLv][0]])) as Sprite;
        icon1.transform.Find("Image").GetComponent<Image>().overrideSprite = sp1;
        Sprite sp2 = Instantiate(Resources.Load<Sprite>("Image/" + LvController.Instance.itemNames[LvController.Instance.itemAdd[LvController.Instance.currentLv][1]])) as Sprite;
        icon2.transform.Find("Image").GetComponent<Image>().overrideSprite = sp2;

        icon1.transform.DOScale(1, 0.5f);
        icon1.transform.DOLocalMoveX(-206, 0.5f);
        icon1.transform.DORotate(new Vector3(0, 0, 360), 0.5f, RotateMode.FastBeyond360)
            .OnComplete(() => {
                icon2.transform.DOScale(1, 0.5f);
                icon2.transform.DOLocalMoveX(206, 0.5f);
                icon2.transform.DORotate(new Vector3(0, 0, 360), 0.5f, RotateMode.FastBeyond360);
            });
    }

    public void Reset()
    {
        icon1.transform.localScale = Vector3.zero;
        icon2.transform.localScale = Vector3.zero;
        icon1.transform.localPosition = new Vector3(0, 174, 894);
        icon2.transform.localPosition = new Vector3(0, 174, 894);
    }
}
